import axios, {type AxiosRequestConfig} from "axios"
import Nprogress from "nprogress"
import "nprogress/nprogress.css"
import {useLoginStore} from "@/store/login";

const request = axios.create({
    baseURL: "/api", // 基础路径
    timeout: 20000 // 超时时间
})

request.interceptors.request.use(function (config:AxiosRequestConfig) {
const loginStore=useLoginStore()


    let token = loginStore.token;
    if (token) {
        config.headers!.token = token;
    }

    Nprogress.start();
    return config;
}, function (error) {
    Nprogress.done();

    return Promise.reject(error);
});


request.interceptors.response.use(function (response) {

    Nprogress.done();

    return response;
}, function (error) {

    Nprogress.done();
    return Promise.reject(error);
});

export default request
